package com.wmc.sqltransformweb.controller;

import com.alibaba.fastjson.JSON;
import com.wmc.sqltransformweb.pojo.request.JavaConfigListRequest;
import com.wmc.sqltransformweb.pojo.request.JavaConfigRequest;
import com.wmc.sqltransformweb.pojo.request.MysqlConfigRequest;
import com.wmc.sqltransformweb.pojo.request.PageRequest;
import com.wmc.sqltransformweb.pojo.response.BaseResponse;
import com.wmc.sqltransformweb.service.SqlTransformService;
import com.wmc.sqltransformweb.utils.ResponseEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @auther wangmingchang
 * @date 2019/10/30 09:47
 */
@RequestMapping("/sqlTransform")
@RestController
@Log4j2
public class SqlTransformJavaController {

    @Autowired
    private SqlTransformService sqlTransformService;

    @PostMapping("/addMysqlConfig")
    @ResponseBody
    public BaseResponse addMysqlConfig(@RequestBody MysqlConfigRequest request){
        request.setUseFlag(true);
        request.setCreateTime(System.currentTimeMillis());
        request.setId(UUID.randomUUID().toString());
        log.info(JSON.toJSONString(request));
        BaseResponse baseResponse = sqlTransformService.addMysqlConfig(request);
        return baseResponse;
    }

    @PostMapping("/getCurrentMysqlConfig")
    @ResponseBody
    public BaseResponse getCurrentMysqlConfig(){
        BaseResponse currentMysqlConfig = sqlTransformService.getCurrentMysqlConfig();
        log.info(JSON.toJSONString(currentMysqlConfig));
        return currentMysqlConfig;
    }

    @PostMapping("/addJavaConfig")
    @ResponseBody
    public BaseResponse addJavaConfig(@RequestBody JavaConfigRequest request){
        request.setUseFlag(true);
        request.setCreateTime(System.currentTimeMillis());
        request.setId(UUID.randomUUID().toString());
        log.info(JSON.toJSONString(request));
        BaseResponse baseResponse = sqlTransformService.addJavaConfig(request);
        return baseResponse;
    }

    @PostMapping("/getCurrentJavaConfig")
    @ResponseBody
    public BaseResponse getCurrentJavaConfig(){
        BaseResponse currentJavaConfig = sqlTransformService.getCurrentJavaConfig();
        log.info(JSON.toJSONString(currentJavaConfig));
        return currentJavaConfig;
    }

    @PostMapping("/addExecute")
    @ResponseBody
    public BaseResponse addExecute(){
        BaseResponse baseResponse = new BaseResponse(ResponseEnum.FAIL);
        try{
            baseResponse = sqlTransformService.addExecute();
        }catch (Exception e){
            log.error("生成异常", e);
        }
        return baseResponse;
    }

    @PostMapping("/queryMsqlConfigList")
    @ResponseBody
    public BaseResponse queryMsqlConfigList(@RequestBody PageRequest pageRequest){
        return sqlTransformService.queryMsqlConfigList(pageRequest);
    }

    @GetMapping("/updateMysqlConfig")
    @ResponseBody
    public BaseResponse updateMysqlConfig(String id){
        BaseResponse baseResponse = new BaseResponse(ResponseEnum.FAIL);
        try{
            baseResponse = sqlTransformService.updateMysqlConfig(id);
        }catch (Exception e){
            log.error("更新异常", e);
        }
        return baseResponse;
    }

    @GetMapping("/deleteMysqlConfig")
    public BaseResponse deleteMysqlConfig(String id){
        BaseResponse baseResponse = new BaseResponse(ResponseEnum.FAIL);
        try{
            baseResponse = sqlTransformService.deleteMysqlConfig(id);
        }catch (Exception e){
            log.error("删除异常", e);
        }
        return baseResponse;
    }
    @PostMapping("/queryJavaConfigList")
    public BaseResponse queryJavalConfigList(@RequestBody JavaConfigListRequest request){
        return sqlTransformService.queryJavalConfigList(request);
    }

    @GetMapping("/updateJavaConfig")
    public BaseResponse updateJavaConfig(String id){
        BaseResponse baseResponse = new BaseResponse(ResponseEnum.FAIL);
        try{
            baseResponse = sqlTransformService.updateJavaConfig(id);
        }catch (Exception e){
            log.error("更新异常", e);
        }
        return baseResponse;
    }

    @GetMapping("/deleteJavaConfig")
    public BaseResponse deleteJavaConfig(String id){
        BaseResponse baseResponse = new BaseResponse(ResponseEnum.FAIL);
        try{
            baseResponse = sqlTransformService.deleteJavaConfig(id);
        }catch (Exception e){
            log.error("删除异常", e);
        }
        return baseResponse;
    }


}
